﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Expression;



namespace Model
{
    public class Foro
    {
        #region Aributos
        private string idForo;
        private string descricao;
        private string telefone;
        private Endereco endereco;
        #endregion

        #region Propriedades
        public string IdForo
        {
            get { return idForo; }
            set { idForo = value; }
        }

        public string Descricao
        {
            get { return descricao; }
            set { descricao = value; }
        }

        public string Telefone
        {
            get { return telefone; }
            set { telefone = value; }
        }

        public Endereco Endereco
        {
            get { return endereco; }
            set { endereco = value; }
        }
        
        #endregion

        #region Construtores

        public Foro()
        {
        }

        public Foro(string pIdForo, string pDescricao, string pTelefone, Endereco pEndereco)
        {
            this.IdForo = pIdForo;
            this.Descricao = pDescricao;
            this.Telefone = pTelefone;
            this.Endereco = pEndereco;
        }

        #endregion

        #region Metodos

        #region Metodos

        #region Persist
        public Boolean Persist()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Save(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Update
        public Boolean Update()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Update(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Delete
        public static Boolean Delete(string pIdForo)
        {
            Foro objForo = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objForo = (Foro)objSession.Load(typeof(Model.Foro), pIdForo);
                objSession.Delete(objForo);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region RetriveObject
        public static Foro RetriveObject(string pIdForo)
        {
            Foro objForo = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objForo = (Foro)objSession.Load(typeof(Model.Foro), pIdForo);
                objSession.Close();
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
            return objForo;
        }
        #endregion

        #region RetriveObjects
        public static IList RetriveObjects()
        {
            IList listForos;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Foro));
                criteria.AddOrder(Order.Asc("Descricao"));

                listForos = criteria.List();

                objSession.Close();
            }
            catch (Exception ex)
            {
                listForos = null;
                objTransaction.Rollback();
                throw ex;
            }
            return listForos;
        }


        public static IList RetriveObjects(string pDescricao)
        {
            IList listForo;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Foro));
                criteria.Add(Expression.Like("Descricao", pDescricao))
                        .AddOrder(Order.Asc("Descricao"));
                listForo = criteria.List();
                objSession.Close();

            }
            catch (Exception ex)
            {
                listForo = null;
                objTransaction.Rollback();
                throw ex;
            }

            return listForo;
        }
        #endregion

        #endregion


        #endregion

    }
}
